package com.controller;

import com.model.BUser;
import com.model.Page;
import com.model.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class adminController {
    @Resource
    com.dao.adminDao adminDao;
//显示登录页面
    @RequestMapping(value = "/adminlogin")
    public String login() {
        return "backend/login";
    }
//提交登录
    @RequestMapping(value = "/doAdminLogin", method = RequestMethod.POST)
    public String adminLogin(BUser bUser, ModelMap model) {
        BUser bUser1 = adminDao.adminLogin(bUser);
        if (bUser1 != null) {
            if(bUser1.getStatus()==1){
                model.addAttribute("msg","你的账号已被禁用");
                return "backend/login";
            }else{
                return "backend/index";
            }
        } else {
            model.addAttribute("msg", "账号或密码错误，请重试输入！");
            return "backend/login";
        }
    }
//获取管理员列表
    @GetMapping(value = "/adminList")
    public String adminList(ModelMap model, Page page){
//        List<BUser> bUserList=adminDao.userList();
//        model.addAttribute("adminList",bUserList);
        List<BUser> userList=adminDao.list(page);
        int total =adminDao.total(page);
        page.caculateLast(total);
        model.addAttribute("adminList",userList);
        model.addAttribute("page",page);
        model.addAttribute("total",total);
        return "backend/admin-list";
    }

    @Resource
    Result result;

    private Result setResult(int status, String msg){
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }
    //添加管理员信息
    @PostMapping(value = "/addAdmin")
    @ResponseBody
    public Result addAdmin(BUser bUser, ModelMap model){
        try{
            int row =adminDao.insertUser(bUser);
            if(row>0){
                return this.setResult(1,"添加管理员成功！");
            }else {
                return this.setResult(2,"添加管理员出错，请联系管理员！");
            }
        }catch (Exception ex){
            return  this.setResult(0,ex.getMessage());
        }
    }
    @GetMapping(value = "/addAdminView")
    public String addAdminView(){
        return "backend/admin-add";
    }


    //修改管理员信息页面
    @GetMapping(value = "/updateAdminView")
    public  String updateAdminView(int id, ModelMap model){
        BUser bUser1 =adminDao.getUserById(id);
        model.addAttribute("bUser",bUser1);
        return "backend/admin-edit";
    }
    //修改管理员信息
    @PostMapping(value = "/updateAdmin")
    @ResponseBody
    public Result updateAdmin(BUser bUser, ModelMap model){
        try {
            int row =adminDao.updateUser(bUser);
            if(row>0){
                return  this.setResult(1,"修改管理员成功！");
            }else {
                return  this.setResult(0,"修改管理员出错，请联系管理员！");
            }
        }catch (Exception ex){
            return this.setResult(0,ex.getMessage());
        }
    }

    //删除管理员
    @PostMapping(value = "/delAdmin")
    @ResponseBody
    public Result delAdmin(Integer id, ModelMap model){
        try{
            BUser bUser=adminDao.getUserById(id);
            if(bUser==null){
                return this.setResult(0,"管理员不存在！");
            }else {
                if(bUser.getSuperuser()==1){
                    return this.setResult(0,"超级管理员不能删除！");
                }else {
                    int row=adminDao.deleteUser(id);
                    if(row>0){
                        return this.setResult(1,"删除管理员成功！");
                    }else {
                        return this.setResult(0,"删除管理员出错，请联系管理员！");
                    }
                }
            }
        }catch (Exception ex){
            return this.setResult(0,"删除管理员出错，请联系管理员！");
        }
    }
    //启用禁用管理员
    @PostMapping(value = "/updateStatus")
    @ResponseBody
    public Result updateStatus(BUser bUser, ModelMap model){
        try {
            BUser bUser1 =adminDao.getUserById(bUser.getId());
            if (bUser1==null){
                return this.setResult(0,"管理员不存在！");
            }else{
                if(bUser1.getSuperuser()==1){
                    return this.setResult(0,"超级管理员不能禁用！");
                }else {
                    int row=adminDao.updateStatus(bUser);
                    if(row>0){
                        return  this.setResult(1,"设置管理员成功！");
                    }else {
                        return  this.setResult(0,"设置管理员出错");
                    }
                }
            }
        }catch (Exception ex){
            return this.setResult(0,ex.getMessage());
        }
    }
}
